package com.xyy.saas.payment.controller;

import com.xyy.saas.payment.adpater.impl.AliCommonAdapter;
import com.xyy.saas.payment.adpater.impl.WxCommonAdpter;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.api.PaymentOrderApi;
import com.xyy.saas.payment.cores.bo.QueryPayNoResultBo;
import com.xyy.saas.payment.cores.bo.QueryResultBo;
import com.xyy.saas.payment.cores.bo.TransactionResultBO;
import com.xyy.saas.payment.cores.enums.InvokerEnum;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.cores.param.TransactionParam;
import com.xyy.saas.payment.cores.vo.QueryPayVo;
import com.xyy.saas.payment.cores.vo.ResultVO;
import com.xyy.saas.payment.query.core.service.QueryTradeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wangtianqi
 * @date 2019/11/5 9:35
 */
/** 查询交易单状态接口 */
@RestController
@RequestMapping(value = {"/api/query"})
public class TradeQueryController {
    private static Logger LOGGER = LoggerFactory.getLogger(TradeQueryController.class);
    @Autowired
    QueryTradeService queryTradeService;
    @Resource
    private PaymentOrderApi paymentOrderApi;
    @Autowired
    private WxCommonAdpter wxCommonAdpter;
    @Autowired
    private AliCommonAdapter aliCommonAdapter;

    /** 查询支付单接口 */
    @RequestMapping(value = {"/onlyQuery"}, method = {RequestMethod.POST})
    public ResultVO<List<QueryPayNoResultBo>> onlyQuery(@RequestBody QueryPayVo queryPayVo){
        try {
            return paymentOrderApi.queryPay(queryPayVo, "");
        } catch (Exception e) {
            LOGGER.error("QueryController#checkPay error", e);
            return ResultVO.create(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    /** 查询微信接口 */
    @RequestMapping(value = {"/wx"}, method = {RequestMethod.POST})
    public ResultVO<TransactionResultBO> wx(@RequestBody TransactionParam param){
        try {
            TransactionResultBO resultBO = wxCommonAdpter.queryTransaction(param);
            return ResultVO.createSuccess(resultBO);
        } catch (Exception e) {
            LOGGER.error("QueryController#checkPay error", e);
            return ResultVO.create(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    /** 查询支付宝接口 */
    @RequestMapping(value = {"/ali"}, method = {RequestMethod.POST})
    public ResultVO<TransactionResultBO> ali(@RequestBody TransactionParam param){
        try {
            TransactionResultBO resultBO = aliCommonAdapter.queryTransaction(param);
            return ResultVO.createSuccess(resultBO);
        } catch (Exception e) {
            LOGGER.error("QueryController#checkPay error", e);
            return ResultVO.create(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    /** 查询支付单接口 */
    @RequestMapping(value = {"/pay"}, method = {RequestMethod.POST})
    public ResultVO<QueryResultBo> checkPay(@RequestParam String tradeNo, @RequestParam String channel, @RequestParam String invoker,@RequestParam String sign){
        try {
            if(!queryTradeService.checkRequestSign(tradeNo, InvokerEnum.valueOf(invoker).getType(), sign)){
                ResultVO.create(ResultCodeEnum.SIGN_ERROR);
            }
            return this.queryTradeService.checkPay(tradeNo, channel, "", "");
        } catch (Exception e) {
            LOGGER.error("QueryController#checkPay error", e);
            return ResultVO.create(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    /** 查询退款单接口 */
    @RequestMapping(value = {"/refund"}, method = {RequestMethod.POST})
    public ResultVO<QueryResultBo> checkRefund(@RequestParam String tradeNo, @RequestParam String invoker, @RequestParam String sign) {
        try {
            if(!queryTradeService.checkRequestSign(tradeNo, InvokerEnum.valueOf(invoker).getType(), sign)){
                ResultVO.create(ResultCodeEnum.SIGN_ERROR);
            }
            return this.queryTradeService.queryRefund(tradeNo);
        } catch (PaymentException e) {
            return ResultVO.create(e.getCodeEnum());
        } catch (Exception e) {
            LOGGER.error("QueryController#checkRefund error", e);
            return ResultVO.create(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

    /** 查询提现单接口 */
    @RequestMapping(value = {"/withdraw"}, method = {RequestMethod.POST})
    public ResultVO<QueryResultBo> checkWithdraw( @RequestParam String tradeNo, @RequestParam String invoker, @RequestParam String sign) {
        try {
            if(!queryTradeService.checkRequestSign(tradeNo, InvokerEnum.valueOf(invoker).getType(), sign)){
                ResultVO.create(ResultCodeEnum.SIGN_ERROR);
            }
            return this.queryTradeService.queryWithdraw(tradeNo);
        } catch (PaymentException e) {
            return ResultVO.create(e.getCodeEnum());
        }catch (Exception e) {
            LOGGER.error("QueryController#checkWithdraw error", e);
            return ResultVO.create(ResultCodeEnum.DEFAULT_ERROR);
        }
    }

}
